package Atomic;

import java.util.concurrent.atomic.AtomicReference;

public class BankCardAtoRefTest {
    private static  AtomicReference<BankCard> bankCardRef = new AtomicReference<>(new BankCard("张三",100)) ;
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                    //先读取全局的应用
                    final BankCard card = bankCardRef.get();
                    //构造一个新的账户，存入一定数量的钱
                    BankCard newCard = new BankCard(card.getAccountName(), card.getMoney() + 100);
                    // 使用 CAS 乐观锁进行非阻塞更新
                    if(bankCardRef.compareAndSet(card, newCard)){
                        System.out.println(newCard);
                    }
            }).start();
        }
    }

}
